<?php
/**
 * Created by PhpStorm.
 * User: HarrisHuang
 * Date: 17/5/12
 * Time: 21:29
 */
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Banner;
use Log;

class BannerController extends Controller
{

    /**
     * 保存banner
     * @param Request $request
     * @param Banner $banner
     */
    public function save(Request $request)
    {
        try {
            $all = $request->all();
            foreach ($all as $v) {
                $banner = new Banner();
                $banner->pic = $v['pic'];
                //type: 1 首页banner,2,信用卡banner
                $banner->type = $v['type'];
                $banner->status = 1;
                $banner->sort = $v['sort'];
                $banner->link = $v['link'];
                $banner->addTime = time();
                $banner->save();
            }
        }catch (\Exception $e){
            Log::error('save banner error '.$e->getMessage());
            return $this->error();
        }
        return $this->success();
    }

    public function update(Request $request)
    {
        try {
            $all = $request->all();
            $ids = array_column($all, 'id');
            //先执行删除
            Banner::where('type', $all[0]['type'])
                ->whereNotIn('id', $ids)
                ->delete();

            //新增或修改
            foreach ($all as $v) {
                if (isset($v['id'])) {
                    Banner::where('id', $v['id'])
                        ->where('type', $v['type'])
                        ->update([
                            'pic' => $v['pic'],
                            'sort' => $v['sort'],
                            'link' => $v['link'],
                        ]);
                } else {
                    Banner::create([
                        'type' => $v['type'],
                        'pic' => $v['pic'],
                        'sort' => $v['sort'],
                        'link' => $v['link'],
                        'addTime' => time()
                    ]);
                }
            }
        }catch (\Exception $e){
            Log::error(' 保存修改banner失败 error='.$e->getMessage());
            return $this->error();
        }
        return $this->success();
    }


    /**
     * banner list
     */
    public function lists(Request $request, Banner $banner)
    {
        $type = (int)$request->get('type', 0);
        $pageSize = $request->get('pageSize',10);
        if($type == 0){
            return $this->error();
        }
        $list = $banner::where('type',$type)->paginate($pageSize);
        $list = $list->toArray();
        $res = [
            'total' => $list['total'],
            'data' => $list['data'],
        ];
        return $this->success($res);
    }

    public function mobileLists(Request $request, Banner $banner)
    {
        $type = (int)$request->get('type', 0);
        $pageSize = $request->get('pageSize',10);
        if($type == 0){
            return $this->error();
        }
        $list = $banner::where('type',$type)->orderBy('sort','desc')->paginate($pageSize);
        $list = $list->toArray();
        $res = [
            'total' => $list['total'],
            'data' => $list['data'],
        ];
        return $this->success($res);
    }


    public function test()
    {
        $res = Banner::whereNotIn('id',[1,7])
            ->delete();
        var_dump($res);
    }


}
